<?php


namespace app\api\controller;


use app\admin\model\Dvideo;
use app\admin\model\Subordinate;
use app\admin\model\Tag;
use app\admin\model\Video;
use app\common\controller\Api;
use think\Db;
use think\Request;

class Sreach extends Api
{
    protected $noNeedLogin = ['*'];

    //社区搜索
    public function community(Request $request)
    {
        if ( ! $request->isGet()) {
            $this->error('ＭＳＴＡ　ＧＥＴ');
        }
        $req = $request->get();
        $res = Video::with('user')->where('status', 1)->where('title', 'like', '%' . $req['title'] . '%')->page($req['current'], $req['every'])->select();
        $res ?$res = $res->toArray():'';
        $this->success('社区搜索结果',$res,200);
    }
    public function video(Request $request)
    {
        if (!$request->isGet()) {
            $this->error('ＭＳＴＡ　ＧＥＴ');
        }
        $req = $request->get();
        $where['status'] = 'normal';
        if (isset($req['title']) && $req['title']) {
            $where['title'] = ['like', '%' . $req['title'] . '%'];
        }
        if (isset($req['tag']) && $req['tag']) {

            $tag = $req['tag'];
            $where['tags'] = ['like', '%' .$tag . '%'];
        }
        if ($req['class']) {
            $where['class'] = $req['class'];
        }

        if ($req['order'] == 1) {
            $order = 'id desc';
        } elseif ($req['order'] == 2) {
            $order = 'hits desc';
        } else {
            $order = 'comments desc';
        }
        $res = Dvideo::where($where)->page($req['current'], $req['every'])->order($order)->select();


        $this->success('视频搜索结果', $res, 200);
    }

    public function getComplex()
    {
        $class = Subordinate::all();
      //  $res ? $res = $res->toArray() : '';
        $data['class'] = $class;
        $data['tags']=Tag::field('id,name')->order('nums desc')->limit(20)->select();
        $data['area']=\app\common\model\Category::where(['type'=>'area','status'=>'normal'])->field('id,name')->select();

        $this->success('success',$data);
    }
}
